package api

import (
	"context"
	"gdmin/servlet"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/cloudwego/hertz/pkg/route"
	"strconv"
)

func DictApi(r *route.RouterGroup) {
	r.POST("/dict/save", createDict)
	r.POST("/dict/update", updateDict)
	r.POST("/dict/page", pageDict)
	r.POST("/dict/tree", treeDict)
	r.POST("/dict/list", listDict)
	r.POST("/dict/byId/:id", byIdDict)
	r.POST("/dict/delete/:id", deleteByIdDict)

	r.POST("/dict_item/save", createDictItem)
	r.POST("/dict_item/update", updateDictItem)
	r.POST("/dict_item/page", pageDictItem)
	r.POST("/dict_item/byId/:id", byIdDictItem)

	return

}

// 字典项列表
// @Tags      字典
// @Summary   字典项列表
// @Produce   application/json
// @Param     name  query       string false  "名称"
// @Param     DictId  query       int false  "字典id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.DictItemResp}  "成功"
// @Router    /dict_item/page [post]
func pageDictItem(ctx context.Context, ctx2 *app.RequestContext) {
	var req servlet.QueryDictItemReq
	err := ctx2.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), ctx2)
		return
	}
	item, err := dictService.PageDictItem(req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), ctx2)
		return
	}
	servlet.OkWithData(ctx2, item)
	return
}

// 根据id删除字典
// @Tags      字典
// @Summary   根据id删除字典
// @Produce   application/json
// @Param id path int true "id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.DeptResp}  "成功"
// @Router    /dict/delete/:id [post]
func deleteByIdDict(ctx context.Context, c *app.RequestContext) {
	dictService.DeleteByDictId(c.Param("id"))
	servlet.Ok(c)
	return

}

// 字典树
// @Tags      字典
// @Summary   字典树
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=model.NodeWithChildren}  "成功"
// @Router    /dict/tree [post]
func treeDict(ctx context.Context, c *app.RequestContext) {
	dict, err := dictService.TreeDict(c.Query("id"))
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, dict)

	return
}

// 字典列表
// @Tags      字典
// @Summary   字典列表
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=model.Dict}  "成功"
// @Router    /dict/list [post]
func listDict(ctx context.Context, c *app.RequestContext) {
	servlet.OkWithData(c, dictService.ListDict())
	return
}

// 分页查询字典
// @Tags      字典
// @Summary   分页查询字典
// @Produce   application/json
// @Param     name  query       string false  "字典名称"
// @Param     code  query       string false  "字典编码"
// @Param     type  query       string false  "字典类型"
// @Param     page_size  query       string false  "页面行数"
// @Param     page_index  query       string false  "页面起始位置"
// @Param     sort  query       string false  "排序规则. 案例: create_time asc , id desc"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.DictResp}  "成功"
// @Router    /dict/page [post]
func pageDict(ctx context.Context, c *app.RequestContext) {
	var req servlet.QueryDictReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	dict, err := dictService.PageDict(&req)
	hlog.Info("dict = ", dict)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &dict)
	return
}

// 根据id查询字典项
// @Tags      字典
// @Summary   根据id查询字典项
// @Produce   application/json
// @Param id path int true "id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.DictItemResp}  "成功"
// @Router    /dict_item/byId/:id [post]
func byIdDictItem(ctx context.Context, c *app.RequestContext) {
	param := c.Param("id")
	num, err := strconv.Atoi(param)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	} else {
		servlet.OkWithData(c, dictService.QueryDictItemById(num))
		return
	}
}

// 更新字典项
// @Tags      字典
// @Summary   更新字典项
// @Produce   application/json
// @Param     data  body      servlet.UpdateDictItemReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.DictItem}  "成功"
// @Router    /dict_item/update [post]
func updateDictItem(ctx context.Context, c *app.RequestContext) {
	var req servlet.UpdateDictItemReq
	err := c.BindAndValidate(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	dictItem, err := dictService.UpdateDictItem(&req)
	hlog.Info("dictItem = ", dictItem)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &dictItem)
	return
}

// 根据id查询字典
// @Tags      字典
// @Summary   根据id查询字典
// @Produce   application/json
// @Param id path int true "id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.DictResp}  "成功"
// @Router    /dict/byId/:id [post]
func byIdDict(ctx context.Context, c *app.RequestContext) {
	param := c.Param("id")
	num, err := strconv.Atoi(param)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	} else {
		servlet.OkWithData(c, dictService.QueryDictById(num))
		return
	}
}

// 创建字典项
// @Tags      字典
// @Summary   创建字典项
// @Produce   application/json
// @Param     data  body      servlet.CreateDictItemReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.DictItem}  "成功"
// @Router    /dict_item/save [post]
func createDictItem(ctx context.Context, c *app.RequestContext) {
	var req servlet.CreateDictItemReq
	err := c.BindAndValidate(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	dictItem, err := dictService.CreateDictItem(&req)
	hlog.Info("dictItem = ", dictItem)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &dictItem)
	return
}

// 更新字典
// @Tags      字典
// @Summary   更新字典
// @Produce   application/json
// @Param     data  body      servlet.UpdateDictReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.Dict}  "成功"
// @Router    /dict/update [post]
func updateDict(ctx context.Context, c *app.RequestContext) {
	var req servlet.UpdateDictReq
	err := c.BindAndValidate(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	dict, err := dictService.UpdateDict(&req)
	hlog.Info("dict = ", dict)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &dict)
	return
}

// 创建字典
// @Tags      字典
// @Summary   创建字典
// @Produce   application/json
// @Param     data  body      servlet.CreateDictReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.Dict}  "成功"
// @Router    /dict/save [post]
func createDict(ctx context.Context, c *app.RequestContext) {
	var req servlet.CreateDictReq
	err := c.BindAndValidate(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	dict, err := dictService.CreateDict(&req)
	hlog.Info("dict = ", dict)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &dict)
	return
}
